home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / swingall.jar / javax / swing / text / StyleContext.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-07-15  |  9.5 KB  |  375 lines

  1. package javax.swing.text;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.FontMetrics;
  6. import java.awt.Toolkit;
  7. import java.io.IOException;
  8. import java.io.NotSerializableException;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.io.Serializable;
  12. import java.util.Enumeration;
  13. import java.util.Hashtable;
  14. import java.util.Vector;
  15. import javax.swing.SwingUtilities;
  16. import javax.swing.event.ChangeListener;
  17.  
  18. public class StyleContext implements Serializable, AbstractDocument.AttributeContext {
  19.    private static StyleContext defaultContext;
  20.    public static final String DEFAULT_STYLE = "default";
  21.    private static Hashtable freezeKeyMap;
  22.    private static Hashtable thawKeyMap;
  23.    private Style styles = new NamedStyle(this, (Style)null);
  24.    private transient FontKey fontSearch = new FontKey((String)null, 0, 0);
  25.    private transient Hashtable fontTable = new Hashtable();
  26.    private transient Hashtable attributesPool = new Hashtable();
  27.    private transient MutableAttributeSet search = new SimpleAttributeSet();
  28.    private int unusedSets;
  29.    static final int THRESHOLD = 9;
  30.    // $FF: synthetic field
  31.    static Class class$javax$swing$event$ChangeListener;
  32.  
  33.    public StyleContext() {
  34.       this.addStyle("default", (Style)null);
  35.    }
  36.  
  37.    public synchronized AttributeSet addAttribute(AttributeSet var1, Object var2, Object var3) {
  38.       if (var1.getAttributeCount() + 1 <= this.getCompressionThreshold()) {
  39.          this.search.removeAttributes(this.search);
  40.          this.search.addAttributes(var1);
  41.          this.search.addAttribute(var2, var3);
  42.          this.reclaim(var1);
  43.          return this.getImmutableUniqueSet();
  44.       } else {
  45.          MutableAttributeSet var4 = this.getMutableAttributeSet(var1);
  46.          var4.addAttribute(var2, var3);
  47.          return var4;
  48.       }
  49.    }
  50.  
  51.    public synchronized AttributeSet addAttributes(AttributeSet var1, AttributeSet var2) {
  52.       if (var1.getAttributeCount() + var2.getAttributeCount() <= this.getCompressionThreshold()) {
  53.          this.search.removeAttributes(this.search);
  54.          this.search.addAttributes(var1);
  55.          this.search.addAttributes(var2);
  56.          this.reclaim(var1);
  57.          return this.getImmutableUniqueSet();
  58.       } else {
  59.          MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
  60.          var3.addAttributes(var2);
  61.          return var3;
  62.       }
  63.    }
  64.  
  65.    public void addChangeListener(ChangeListener var1) {
  66.       this.styles.addChangeListener(var1);
  67.    }
  68.  
  69.    public Style addStyle(String var1, Style var2) {
  70.       NamedStyle var3 = new NamedStyle(this, var1, var2);
  71.       if (var1 != null) {
  72.          this.styles.addAttribute(var1, var3);
  73.       }
  74.  
  75.       return var3;
  76.    }
  77.  
  78.    // $FF: synthetic method
  79.    static Class class$(String var0) {
  80.       try {
  81.          return Class.forName(var0);
  82.       } catch (ClassNotFoundException var2) {
  83.          throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  84.       }
  85.    }
  86.  
  87.    protected MutableAttributeSet createLargeAttributeSet(AttributeSet var1) {
  88.       return new SimpleAttributeSet(var1);
  89.    }
  90.  
  91.    protected SmallAttributeSet createSmallAttributeSet(AttributeSet var1) {
  92.       return new SmallAttributeSet(this, var1);
  93.    }
  94.  
  95.    public Color getBackground(AttributeSet var1) {
  96.       return StyleConstants.getForeground(var1);
  97.    }
  98.  
  99.    protected int getCompressionThreshold() {
  100.       return 9;
  101.    }
  102.  
  103.    public static final StyleContext getDefaultStyleContext() {
  104.       if (defaultContext == null) {
  105.          defaultContext = new StyleContext();
  106.       }
  107.  
  108.       return defaultContext;
  109.    }
  110.  
  111.    public AttributeSet getEmptySet() {
  112.       return SimpleAttributeSet.EMPTY;
  113.    }
  114.  
  115.    public Font getFont(String var1, int var2, int var3) {
  116.       this.fontSearch.setValue(var1, var2, var3);
  117.       Font var4 = (Font)this.fontTable.get(this.fontSearch);
  118.       if (var4 == null) {
  119.          var4 = new Font(var1, var2, var3);
  120.          FontKey var5 = new FontKey(var1, var2, var3);
  121.          this.fontTable.put(var5, var4);
  122.       }
  123.  
  124.       return var4;
  125.    }
  126.  
  127.    public Font getFont(AttributeSet var1) {
  128.       int var2 = 0;
  129.       if (StyleConstants.isBold(var1)) {
  130.          var2 |= 1;
  131.       }
  132.  
  133.       if (StyleConstants.isItalic(var1)) {
  134.          var2 |= 2;
  135.       }
  136.  
  137.       String var3 = StyleConstants.getFontFamily(var1);
  138.       int var4 = StyleConstants.getFontSize(var1);
  139.       if (StyleConstants.isSuperscript(var1) || StyleConstants.isSubscript(var1)) {
  140.          var4 -= 2;
  141.       }
  142.  
  143.       return this.getFont(var3, var2, var4);
  144.    }
  145.  
  146.    public FontMetrics getFontMetrics(Font var1) {
  147.       return Toolkit.getDefaultToolkit().getFontMetrics(var1);
  148.    }
  149.  
  150.    public Color getForeground(AttributeSet var1) {
  151.       return StyleConstants.getForeground(var1);
  152.    }
  153.  
  154.    AttributeSet getImmutableUniqueSet() {
  155.       SmallAttributeSet var1 = this.createSmallAttributeSet(this.search);
  156.       SmallAttributeSet var2 = (SmallAttributeSet)this.attributesPool.get(var1);
  157.       if (var2 == null) {
  158.          var2 = var1;
  159.          this.attributesPool.put(var1, var1);
  160.       }
  161.  
  162.       ++var2.nrefs;
  163.       return var2;
  164.    }
  165.  
  166.    MutableAttributeSet getMutableAttributeSet(AttributeSet var1) {
  167.       return var1 instanceof MutableAttributeSet && var1 != SimpleAttributeSet.EMPTY ? (MutableAttributeSet)var1 : this.createLargeAttributeSet(var1);
  168.    }
  169.  
  170.    public static Object getStaticAttribute(Object var0) {
  171.       return thawKeyMap != null && var0 != null ? thawKeyMap.get(var0) : null;
  172.    }
  173.  
  174.    public static Object getStaticAttributeKey(Object var0) {
  175.       return var0.getClass().getName() + "." + var0.toString();
  176.    }
  177.  
  178.    public Style getStyle(String var1) {
  179.       return (Style)this.styles.getAttribute(var1);
  180.    }
  181.  
  182.    public Enumeration getStyleNames() {
  183.       return this.styles.getAttributeNames();
  184.    }
  185.  
  186.    public static void readAttributeSet(ObjectInputStream var0, MutableAttributeSet var1) throws ClassNotFoundException, IOException {
  187.       int var2 = var0.readInt();
  188.  
  189.       for(int var3 = 0; var3 < var2; ++var3) {
  190.          Object var4 = var0.readObject();
  191.          Object var5 = var0.readObject();
  192.          if (thawKeyMap != null) {
  193.             Object var6 = thawKeyMap.get(var4);
  194.             if (var6 != null) {
  195.                var4 = var6;
  196.             }
  197.  
  198.             Object var7 = thawKeyMap.get(var5);
  199.             if (var7 != null) {
  200.                var5 = var7;
  201.             }
  202.          }
  203.  
  204.          var1.addAttribute(var4, var5);
  205.       }
  206.  
  207.    }
  208.  
  209.    public void readAttributes(ObjectInputStream var1, MutableAttributeSet var2) throws ClassNotFoundException, IOException {
  210.       readAttributeSet(var1, var2);
  211.    }
  212.  
  213.    private void readObject(ObjectInputStream var1) throws ClassNotFoundException, IOException {
  214.       this.fontSearch = new FontKey((String)null, 0, 0);
  215.       this.fontTable = new Hashtable();
  216.       this.search = new SimpleAttributeSet();
  217.       this.attributesPool = new Hashtable();
  218.       var1.defaultReadObject();
  219.    }
  220.  
  221.    public void reclaim(AttributeSet var1) {
  222.       if (var1 instanceof SmallAttributeSet) {
  223.          SmallAttributeSet var2 = (SmallAttributeSet)var1;
  224.          --var2.nrefs;
  225.          if (var2.nrefs <= 0) {
  226.             ++this.unusedSets;
  227.             if (this.unusedSets > 10 && this.unusedSets > this.attributesPool.size() / 10) {
  228.                if (SwingUtilities.isEventDispatchThread()) {
  229.                   this.removeUnusedSets();
  230.                } else {
  231.                   1 var3 = new 1(this);
  232.                   SwingUtilities.invokeLater(var3);
  233.                }
  234.             }
  235.          }
  236.       }
  237.  
  238.    }
  239.  
  240.    public static void registerStaticAttributeKey(Object var0) {
  241.       String var1 = var0.getClass().getName() + "." + var0.toString();
  242.       if (freezeKeyMap == null) {
  243.          freezeKeyMap = new Hashtable();
  244.          thawKeyMap = new Hashtable();
  245.       }
  246.  
  247.       freezeKeyMap.put(var0, var1);
  248.       thawKeyMap.put(var1, var0);
  249.    }
  250.  
  251.    public synchronized AttributeSet removeAttribute(AttributeSet var1, Object var2) {
  252.       if (var1.getAttributeCount() - 1 <= this.getCompressionThreshold()) {
  253.          this.search.removeAttributes(this.search);
  254.          this.search.addAttributes(var1);
  255.          this.search.removeAttribute(var2);
  256.          this.reclaim(var1);
  257.          return this.getImmutableUniqueSet();
  258.       } else {
  259.          MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
  260.          var3.removeAttribute(var2);
  261.          return var3;
  262.       }
  263.    }
  264.  
  265.    public synchronized AttributeSet removeAttributes(AttributeSet var1, Enumeration var2) {
  266.       if (var1.getAttributeCount() <= this.getCompressionThreshold()) {
  267.          this.search.removeAttributes(this.search);
  268.          this.search.addAttributes(var1);
  269.          this.search.removeAttributes(var2);
  270.          this.reclaim(var1);
  271.          return this.getImmutableUniqueSet();
  272.       } else {
  273.          MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
  274.          var3.removeAttributes(var2);
  275.          return var3;
  276.       }
  277.    }
  278.  
  279.    public synchronized AttributeSet removeAttributes(AttributeSet var1, AttributeSet var2) {
  280.       if (var1.getAttributeCount() <= this.getCompressionThreshold()) {
  281.          this.search.removeAttributes(this.search);
  282.          this.search.addAttributes(var1);
  283.          this.search.removeAttributes(var2);
  284.          this.reclaim(var1);
  285.          return this.getImmutableUniqueSet();
  286.       } else {
  287.          MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
  288.          var3.removeAttributes(var2);
  289.          return var3;
  290.       }
  291.    }
  292.  
  293.    public void removeChangeListener(ChangeListener var1) {
  294.       this.styles.removeChangeListener(var1);
  295.    }
  296.  
  297.    public void removeStyle(String var1) {
  298.       this.styles.removeAttribute(var1);
  299.    }
  300.  
  301.    synchronized void removeUnusedSets() {
  302.       Vector var1 = new Vector();
  303.       Enumeration var2 = this.attributesPool.keys();
  304.  
  305.       while(var2.hasMoreElements()) {
  306.          SmallAttributeSet var3 = (SmallAttributeSet)var2.nextElement();
  307.          if (var3.nrefs <= 0) {
  308.             var1.addElement(var3);
  309.          }
  310.       }
  311.  
  312.       var2 = var1.elements();
  313.  
  314.       while(var2.hasMoreElements()) {
  315.          this.attributesPool.remove(var2.nextElement());
  316.       }
  317.  
  318.       this.unusedSets = 0;
  319.    }
  320.  
  321.    public String toString() {
  322.       this.removeUnusedSets();
  323.       String var1 = "";
  324.  
  325.       SmallAttributeSet var3;
  326.       for(Enumeration var2 = this.attributesPool.keys(); var2.hasMoreElements(); var1 = var1 + var3 + "\n") {
  327.          var3 = (SmallAttributeSet)var2.nextElement();
  328.       }
  329.  
  330.       return var1;
  331.    }
  332.  
  333.    public static void writeAttributeSet(ObjectOutputStream var0, AttributeSet var1) throws IOException {
  334.       int var2 = var1.getAttributeCount();
  335.       var0.writeInt(var2);
  336.       Enumeration var3 = var1.getAttributeNames();
  337.  
  338.       while(var3.hasMoreElements()) {
  339.          Object var4 = var3.nextElement();
  340.          if (var4 instanceof Serializable) {
  341.             var0.writeObject(var4);
  342.          } else {
  343.             Object var5 = freezeKeyMap.get(var4);
  344.             if (var5 == null) {
  345.                throw new NotSerializableException(var4.getClass().getName() + " is not serializable as a key in an AttributeSet");
  346.             }
  347.  
  348.             var0.writeObject(var5);
  349.          }
  350.  
  351.          Object var7 = var1.getAttribute(var4);
  352.          if (var7 instanceof Serializable) {
  353.             var0.writeObject(var7);
  354.          } else {
  355.             Object var6 = freezeKeyMap.get(var7);
  356.             if (var6 == null) {
  357.                throw new NotSerializableException(var7.getClass().getName() + " is not serializable as a value in an AttributeSet");
  358.             }
  359.  
  360.             var0.writeObject(var6);
  361.          }
  362.       }
  363.  
  364.    }
  365.  
  366.    public void writeAttributes(ObjectOutputStream var1, AttributeSet var2) throws IOException {
  367.       writeAttributeSet(var1, var2);
  368.    }
  369.  
  370.    private void writeObject(ObjectOutputStream var1) throws IOException {
  371.       this.removeUnusedSets();
  372.       var1.defaultWriteObject();
  373.    }
  374. }
  375.